﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Ewk.Runtime.Serialization.CustomDatafieldSerializers.PositionBased;
using Ewk.Runtime.Serialization.PositionBased;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace CustomDatafieldSerializers.UnitTests.PositionBased
{
    [TestClass]
    public class BooleanSerializerTests
    {
        [TestMethod]
        public void BooleanSerializer_can_serialize_a_boolean_value_to_the_correct_string_representation_using_the_Format()
        {
            AssertSerializedValue("Y", "YN", true);
            AssertSerializedValue("Y", "Y", true);
            AssertSerializedValue("Y", "y", true);
            AssertSerializedValue("Y", "yn", true);

            AssertSerializedValue("N", "YN", false);
            AssertSerializedValue("N", "Y", false);
            AssertSerializedValue("N", "y", false);
            AssertSerializedValue("N", "yn", false);

            AssertSerializedValue("1", "1", true);
            AssertSerializedValue("0", "1", false);
        }

        [TestMethod]
        public void BooleanSerializer_can_deserialize_a_string_representation_of_a_boolean_to_the_correct_boolean_value_using_the_Format()
        {
            AssertDeserializedValue(true, "YN", "Y");
            AssertDeserializedValue(true, "Y", "Y");
            AssertDeserializedValue(true, "y", "Y");
            AssertDeserializedValue(true, "yn", "Y");

            AssertDeserializedValue(false, "YN", "N");
            AssertDeserializedValue(false, "Y", "N");
            AssertDeserializedValue(false, "y", "N");
            AssertDeserializedValue(false, "yn", "N");

            AssertDeserializedValue(true, "1", "1");
            AssertDeserializedValue(false, "1", "0");
        }

        static void AssertSerializedValue(string expected, string format, bool value)
        {
            var attribute = new PositionBasedDatafieldAttribute(0, 1)
            {
                Format = format,
                SerializerType = typeof(BooleanSerializer),
            };

            var serializer = new BooleanSerializer();
            var result = serializer.Serialize(value, attribute);

            Assert.AreEqual(expected, result);
        }

        static void AssertDeserializedValue(bool expected, string format, string value)
        {
            var attribute = new PositionBasedDatafieldAttribute(0, 1)
            {
                Format = format,
                SerializerType = typeof(BooleanSerializer),
            };

            var serializer = new BooleanSerializer();
            var result = serializer.Deserialize(value, attribute);

            Assert.AreEqual(expected, result);
        }
    }
}
